<?php
class Sysadd extends  Controller
{
	function Sysadd()
    {
        parent::Controller();
      	$this->userdata = checksession($this->session,"systemdata");
		header("Content-Type: text/html; charset=utf-8");
	  	

    }
    
    
    function city()
    {
    
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"areaid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("area"),
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'areaid',
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."sysadd/city/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("sysadd_city_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","城市列表");
    	 	  $this->mytp->tp("sysadd_city_list");
    	 }
	    	
    	 
    	
    }
    function addcity()
    {
    	$this->mytp->assign("page_title","添加城市");
    	$this->mytp->tp("sysadd_city_add");
    }
    function savecity()
    {
    	$city = $_POST["city"];
    	$data = array(
			    	'area'=>$city,
					'sorts'=>50
					);
    	$this->db->insert('area',$data);
    	s("添加成功",baseurl."sysadd/city");
    }
    
    function company()
    {
    
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"c.companyid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("company")." as c left join ".table("area")." as a on c.areaid=a.areaid",
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'c.companyid',
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."sysadd/company/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("sysadd_company_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","公司列表");
    	 	  $this->mytp->tp("sysadd_company_list");
    	 }
	    	
    	 
    	
    }
    
    function addcompany()
    {
    	$query = $this->db->get("area");
    	$rs = $query->result_array();
    	$city = array();
    	foreach ($rs as $v)
    	{
    		$city[$v["areaid"]] = $v["area"];
    	}
    	
    	$this->mytp->assign("city",$city);
    	$this->mytp->assign("page_title","添加公司");
    	$this->mytp->tp("sysadd_company_add");
    }
    function savecompany()
    {
    	$city = $_POST["city"];
    	$company = $_POST["company"];
    	$data = array(
			    	'areaid'=>$city,
			    	'company'=>$company,
					'sorts'=>50
					);
    	$this->db->insert('company',$data);
    	s("添加成功",baseurl."sysadd/company");
    }
    function invitecode()
    {
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"inviteid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("invite"),
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'inviteid',
		     'where'=>'state=0',
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."sysadd/invitecode/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("sysadd_invitecode_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","邀请码列表");
    	 	  $this->mytp->tp("sysadd_invitecode_list");
    	 }
	    	
    }
    function addinvitecode()
    {
    	$rand = time().rand(0,1000);
    	$data = array(
			    	'userid'=>0,
					'code'=>md5($rand),
					'state'=>0,
					);
    	$this->db->insert('invite',$data);
    	s("添加成功",baseurl."sysadd/invitecode");
    }
    function eatery()
    {
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"eateryid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("eatery"),
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'eateryid',
			  'where'=>'areaid='.$this->userdata["areaid"],
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."sysadd/eatery/?sort_by=".$sort_by."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);
         //A($this->jypage->sql);
    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("sysadd_eatery_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","餐馆列表");
    	 	  $this->mytp->tp("sysadd_eatery_list");
    	 }
    }
    function eateryadd()
    {
    	$id = empty($_REQUEST["id"])?"":$_REQUEST["id"];
    	
    	$rs = array("sendstart"=>0);
    	if(!empty($id))
    	{
    		$query = $this->db->query("select * from ".table("eatery")." where eateryid=$id");
    		$rs = $query->row_array();
    		
    		
    	}
    	$this->mytp->assign("rs",$rs);
    	$this->mytp->assign("page_title","添加餐馆");
    	$this->mytp->tp("sysadd_eatery_add");
    }
    
    function eaterydel()
    {
    	$id = empty($_REQUEST["id"])?"":$_REQUEST["id"];
    	if(!empty($id))
    	{
    		$this->db->query("delete from ".table("food")." where eatid=$id");
    		$this->db->query("delete from ".table("eatery")." where eateryid=$id");
    		s("删除成功",baseurl."sysadd/eatery");
    	}
    	
    }
    function eaterysave()
    {
    	$id = $_POST["id"];
    	$data = array(
			    	'eatname'=>$_POST["eatname"],
					'tel'=>$_POST["tel"],
					'areaid'=>$this->userdata["areaid"],
					'place'=>$_POST["place"],
					'foodtime'=>0,
					'sendstart'=>$_POST["sendstart"],
					'sendcost'=>$_POST["sendcost"],
    				'remark'=>$_POST["remark"]
					);
    	if(empty($id))
    	{
    		
			$this->db->insert('eatery',$data);
			$l = "添加成功";
    	}else
    	{
    		$this->db->where('eateryid',$id);
    		$this->db->update('eatery',$data);
    		$l = "修改成功";
    	}
    	
    	
    	s($l,baseurl."sysadd/eatery");
    }
    function foodlist()
    {
    	 $is_ajax = empty($_REQUEST["is_ajax"])?0:$_REQUEST["is_ajax"];
    	 $cid = empty($_REQUEST["cid"])?0:$_REQUEST["cid"];
    	 $sort_by = empty($_REQUEST["sort_by"])?"f.foodid":$_REQUEST["sort_by"];
    	 $sort_order = empty($_REQUEST["sort_order"])?"DESC":$_REQUEST["sort_order"];
    	 $page = empty($_REQUEST["page"])?1:$_REQUEST["page"];
    	 
		 $this->jypage->SetAttr(array(
			  'table'=>table("food")." as f left join ".table("foodsort")." as s on f.sortid=s.sortid",
			  'pagesize'=>10,
			  'page'=>$page,
			  'key'=>'f.foodid',
			  'where'=>'f.eatid='.$cid,
			  "field"=>array("f.*","s.names"),
			  'condition'=>' order by '.$sort_by." ".$sort_order,
			  'pageurl'=>baseurl."sysadd/foodlist/?sort_by=".$sort_by."&cid=".$cid."&sort_order=".$sort_order."&page=",
			  ));
		 $this->jypage->SetDb($this->db);
		
		 $pagelist = $this->jypage->pages();
		 $pagestr = $this->jypage->SetPageStr();
    	
		 $this->mytp->assign("pagestr",$pagestr);
		 $this->mytp->assign("pagelist",$pagelist["ZeroPage"]);
		 $this->mytp->assign("cid",$cid);
    	
    	 $this->mytp->assign("full_page",$is_ajax==1?0:1);

    	 if($is_ajax)
    	 {
    	 	 
    	 	 $result = array("error"=>"0","content"=>"","message"=>"");
    	 	 $result["content"] = $this->mytp->fetch("sysadd_food_list.htm");
    	 	 echo json_encode($result);
    	 }else
    	 {
    	 	  $this->mytp->assign("page_title","菜品管理");
    	 	  $this->mytp->tp("sysadd_food_list");
    	 }
    }
    function foodadd()
    {
    	$cid = empty($_REQUEST["cid"])?0:$_REQUEST["cid"];
    	$fid = empty($_REQUEST["fid"])?0:$_REQUEST["fid"];
    	$this->mytp->assign("page_title","添加菜品");
    	$query = $this->db->get("foodsort");
    	$rs = $query->result_array();
    	$sort = array();
    	foreach ($rs as $v)
    	{
    		$sort[$v["sortid"]] = $v["names"];
    	}
    	if(!empty($fid))
    	{
    		$q =  $this->db->query("select * from ".table("food")." where foodid=$fid");
    		
    		$food =  $q->row_array();
    		
    		$this->mytp->assign("food",$food);
    		
    	}
    	$this->mytp->assign("sort",$sort);
    	$this->mytp->assign("cid",$cid);
    	$this->mytp->tp("sysadd_food_add");
    }
    function foodsave()
    {
    	$cid = $_POST["cid"];
    	$fid = $_POST["fid"];
    	if($fid>0)
    	{
    		$data = array(
					'sortid'=>$_POST["sort"],
					'foodname'=>$_POST["foodname"],
					'price'=>$_POST["price"]
					);
			$this->db->where('foodid',$fid);
			$this->db->update('food',$data);
    		$l = "修改成功";
    	}else
    	{
    		$data = array(
			    	'eatid'=>$cid,
					'sortid'=>$_POST["sort"],
					'foodname'=>$_POST["foodname"],
					'price'=>$_POST["price"]
					);
			$this->db->insert('food',$data);
			$l = "添加成功";
    	}
    	
    	
    	s($l,baseurl."sysadd/foodadd/?cid=".$cid);
    }
    function fooddel()
    {
    	$fid = empty($_REQUEST["fid"])?0:$_REQUEST["fid"];
    	$cid = empty($_REQUEST["cid"])?0:$_REQUEST["cid"];
    	if($fid>0 && $cid>0)
    	{
    		$this->db->query("delete from ".table("food")." where foodid=".$fid);
    		s("删除成功",baseurl."sysadd/foodlist/?cid=".$cid);
    	}
    }
    function sortadd()
    {
    	$sortname = $_POST["sortname"];
    	$result = array("error"=>1,"message"=>"");
    	
    	if($sortname!="")
    	{
    		$query1 = $this->db->query("select sortid from ".table("foodsort")." where names='".$sortname."'");
    		if(!$query1->num_rows()>0)
    		{
    			$this->db->query("insert into ".table("foodsort")."(names) values('".$sortname."')");
    		
	    		$query = $this->db->query("select * from ".table("foodsort"));
	    		$rs = $query->result();
	    		if(is_array($rs))
	    		{
	    			
	    			$result = array("error"=>0,"message"=>$rs);
	    			
	    		}
    		}else
    		{
    			$result = array("message"=>"该类别已经存在");
    		}
    		
    	}
    	echo json_encode($result);
    	
    }
    
}    
?>